﻿Imports System.Threading
Public Class Form1

    Dim _imgHitArea As Point = New Point(15, 2)                                 ' create area
    Dim _imageLocation As Point = New Point(20, 5)                              ' create image

    Public Sub New()
        InitializeComponent()
        tabMain.DrawMode = System.Windows.Forms.TabDrawMode.OwnerDrawFixed      'Set the Mode of Drawing as Owner Drawn
        AddHandler tabMain.DrawItem, AddressOf TabControl1_DrawItem
        ' Me.StartPosition = FormStartPosition.CenterScreen                       'Set Center Screen
        ' =============== Hide TabPages =============
        Me.tabMain.Controls.Remove(tabCompany)
        Me.tabMain.Controls.Remove(tabCalendar)
        Me.tabMain.Controls.Remove(tabDepartment)
        Me.tabMain.Controls.Remove(tabHoliday)
        Me.tabMain.Controls.Remove(tabEmployee)
        Me.tabMain.Controls.Remove(tabWorkdata)
        Me.tabMain.Controls.Remove(tabWorktype)
        Me.tabMain.Controls.Remove(tabOutput)
        Me.tabMain.Controls.Remove(tabTerminal)
        Me.tabMain.Controls.Remove(tabTimetable)
        Me.tabMain.Hide()
    End Sub

    '======================= Draw CloseButton=========================
    Private Sub TabControl1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs)
        Try
            'Close Image to draw
            Dim img As Image = New Bitmap("E:\JCS\Project SAT\Interface\gtk_close.png")
            Dim r As Rectangle = e.Bounds

            r = Me.tabMain.GetTabRect(e.Index)
            r.Offset(2, 2)
            Dim TitleBrush As Brush = New SolidBrush(Color.Black)
            Dim f As Font = Me.Font
            Dim title As String = Me.tabMain.TabPages(e.Index).Text
            e.Graphics.DrawString(title, f, TitleBrush, New PointF(r.X, r.Y))
            e.Graphics.DrawImage(img, New Point(r.X + (Me.tabMain.GetTabRect(e.Index).Width - _imageLocation.X), _imageLocation.Y))

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    '======================== Check tab open ======================
    Public Function checktabmain() As Boolean
        Dim check As Boolean
        check = False
        If tabMain.TabPages.Count = 0 Then
            Return True
        End If
        Return False
    End Function
    '======================== Close Tab ========================
    Private Sub TabControl1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles tabMain.MouseClick
        Dim tc As TabControl = CType(sender, TabControl)
        Dim p As Point = e.Location
        Dim _tabWidth As Integer

        _tabWidth = Me.tabMain.GetTabRect(tc.SelectedIndex).Width - (_imgHitArea.X)
        Dim r As Rectangle = Me.tabMain.GetTabRect(tc.SelectedIndex)
        r.Offset(_tabWidth, _imgHitArea.Y)
        r.Width = 10
        r.Height = 10
        If r.Contains(p) Then
            Dim TabP As TabPage = DirectCast(tc.TabPages.Item(tc.SelectedIndex), TabPage)
            tc.TabPages.Remove(TabP)
        End If
        If checktabmain() Then
            tabMain.Hide()
        End If
    End Sub
    '===============Check exist TabPages =======================
    Public Function checktab(ByVal tabname As String) As Boolean
        Dim check As Boolean
        check = True
        Dim i As Integer
        For i = 0 To tabMain.TabCount - 1 Step 1
            'MsgBox(tabMain.TabPages.Item(i).Text)
            If tabMain.TabPages.Item(i).Name.Equals(tabname) Then
                check = False
            End If
        Next
        'MsgBox(check)
        Return check
    End Function
    '' ================ Open tab ========================
    Private Sub CompanyToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CompanyToolStripMenuItem.Click
        Me.tabMain.Show()
        If checktab("tabCompany") Then
            Me.tabMain.Controls.Add(tabCompany)
        End If
        Me.tabMain.SelectedTab = tabCompany                 ' Selects tabCompany using tabMain
    End Sub

    Private Sub DepartmentToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DepartmentToolStripMenuItem.Click
        Me.tabMain.Show()
        If checktab("tabDepartment") Then
            Me.tabMain.Controls.Add(tabDepartment)
        End If
        Me.tabMain.SelectedTab = tabDepartment

    End Sub

    Private Sub HolidayToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HolidayToolStripMenuItem.Click
        Me.tabMain.Show()
        If checktab("tabHoliday") Then
            Me.tabMain.Controls.Add(tabHoliday)
        End If
        Me.tabMain.SelectedTab = tabHoliday
    End Sub

    Private Sub CalendarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalendarToolStripMenuItem.Click
        Me.tabMain.Show()
        If checktab("tabCalendar") Then
            Me.tabMain.Controls.Add(tabCalendar)
        End If
        Me.tabMain.SelectedTab = tabCalendar
    End Sub

    Private Sub WorktypeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WorktypeToolStripMenuItem.Click
        Me.tabMain.Show()
        If checktab("tabWorktype") Then
            Me.tabMain.Controls.Add(tabWorktype)
        End If
        Me.tabMain.SelectedTab = tabWorktype
    End Sub

    Private Sub EmployeeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EmployeeToolStripMenuItem.Click
        Me.tabMain.Show()
        If checktab("tabEmployee") Then
            Me.tabMain.Controls.Add(tabEmployee)
        End If
        Me.tabMain.SelectedTab = tabEmployee
    End Sub

    Private Sub FTTerminalToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FTTerminalToolStripMenuItem.Click
        Me.tabMain.Show()
        If checktab("tabTerminal") Then
            Me.tabMain.Controls.Add(tabTerminal)
        End If
        Me.tabMain.SelectedTab = tabTerminal
    End Sub

    Private Sub TimetableToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimetableToolStripMenuItem.Click
        Me.tabMain.Show()
        If checktab("tabTimetable") Then
            Me.tabMain.Controls.Add(tabTimetable)
        End If
        Me.tabMain.SelectedTab = tabTimetable
    End Sub

    Private Sub WorkdataToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WorkdataToolStripMenuItem.Click
        Me.tabMain.Show()
        If checktab("tabWorkdata") Then
            Me.tabMain.Controls.Add(tabWorkdata)
        End If
        Me.tabMain.SelectedTab = tabWorkdata
    End Sub

    Private Sub OutputOptionToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OutputOptionToolStripMenuItem.Click
        Me.tabMain.Show()
        If checktab("tabOutput") Then
            Me.tabMain.Controls.Add(tabOutput)
        End If
        Me.tabMain.SelectedTab = tabOutput
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        'Dim i As Integer
        'For i = 0 To tabMain.TabCount - 1 Step 1
        '    'MsgBox(tabMain.TabPages.Item(0).Name)
        '    'Thread.Sleep(1000)
        '    tabMain.TabPages.Item(0).Dispose()
        'Next
        'If checktabmain() Then
        '    tabMain.Hide()
        'End If
        Me.Close()
    End Sub
End Class